package dao;

import model.User;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl implements UserDAO {

    // 用户登录验证
    @Override
    public User findByAccountAndPassword(String account, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        User user = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM users WHERE account = ? AND userPwd = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, account);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();

            // 打印日志
            System.out.println("Executing SQL: " + sql);
            System.out.println("Parameters: account=" + account + ", password=" + password);

            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setAccount(rs.getString("account"));
                user.setUserPwd(rs.getString("userPwd"));
                user.setUserName(rs.getString("userName"));
                user.setSex(rs.getString("sex"));
                user.setIntroduction(rs.getString("introduction"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        return user;
    }

    // 添加用户
    @Override
    public int insert(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int result = 0;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO users (account, userPwd, userName, sex, introduction) " +
                    "VALUES (?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getAccount());
            pstmt.setString(2, user.getUserPwd());
            pstmt.setString(3, user.getUserName());
            pstmt.setString(4, user.getSex());
            pstmt.setString(5, user.getIntroduction());

            // 打印日志
            System.out.println("Executing SQL: " + sql);
            System.out.println("Parameters: account=" + user.getAccount() + ", userName=" + user.getUserName() + ", userPwd=" + user.getUserPwd() + ", sex=" + user.getSex() + ", introduction=" + user.getIntroduction());

            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("SQL Error: " + e.getMessage()); // 打印 SQL 错误信息
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
        return result;
    }

    // 修改用户信息
    @Override
    public int update(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int result = 0;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE users SET account=?, userPwd=?, userName=?, sex=?, introduction=? " +
                    "WHERE userId=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getAccount());
            pstmt.setString(2, user.getUserPwd());
            pstmt.setString(3, user.getUserName());
            pstmt.setString(4, user.getSex());
            pstmt.setString(5, user.getIntroduction());
            pstmt.setInt(6, user.getUserId());
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, null);
        }
        return result;
    }

    // 模糊查找用户
    @Override
    public List<User> findByKeyword(String keyword) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM users WHERE userId LIKE ? OR userName LIKE ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "%" + keyword + "%");
            pstmt.setString(2, "%" + keyword + "%");
            rs = pstmt.executeQuery();

            while (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("userId"));
                user.setAccount(rs.getString("account"));
                user.setUserPwd(rs.getString("userPwd"));
                user.setUserName(rs.getString("userName"));
                user.setSex(rs.getString("sex"));
                user.setIntroduction(rs.getString("introduction"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }
        return users;
    }
}